// pages/ScenicArea/ScenicAreaDetail/index.js
const common = require('../../../public.js')
const config = require('../../../config')
const utils = require('../utils.js')
const app = getApp()
Page({
    data: {
        isOverShare: true,  //全局分享标识请勿删除
        storeInfo: {},
        moduleType: '', //模块类型 1 = 景区景点  2 = 文化场馆   后加的 3 = 运动健身
        currentPrebuy: {}, // 点击购买须知存当前对象
        currentBanner: 1,
        ticketShowNum: 4,
        showMustKnow: false,
        showCouponsPanel: false,
        articleList: [], // 只有 moduleType=2 有数据
        isLoadingBusy: false,
        ticketName: '',
        isFyApp: config.isFyApp,
        active: 0,
        name: "",
        typeNum: '',  //文艺场馆
        tabs: '',
        bottoms: 0,  //适配机型
    },

    onLoad: function (options) {
        var that = this
        this.setData({
            name: options.storeTypeName,
            typeNum: options.storeType
        })
        if (options.q) {
            let scan_url = decodeURIComponent(options.q);
            let video_id = scan_url.match(/\d+/) //提取链接中的数字，也就是链接中的参数id，/\d+/ 为正则表达式
            this.setData({
                isLoadingBusy: true,
                storeId: video_id[0],
            }, _ => {
                this.getStoreInfo()
                this.getInfo()
            })
            return
        }

        if (this.data.isFyApp) {
            // 飞羊小程序显示的是页面，展示返回按钮
            const capsule = wx.getMenuButtonBoundingClientRect()
            this.setData({
                topOffset: capsule.top,
                height: capsule.height
            })
        }

        that.setData({
            isLoadingBusy: true,
            storeId: options.id ? options.id : config.id,
            // storeId: 4133,// 2439 没数据  4133 有数据   415测试小店专用
            moduleType: options.moduleType ? options.moduleType : config.type
        }, _ => {
            that.data.moduleType == 2 ? that.setData({ tabs: 'venue' }, () => {
                var pages = getCurrentPages() //获取加载的页面
                var currentPage = pages[pages.length - 1] //获取当前页面的对象
                var url = currentPage.route //当前页面url 

            }) : that.setData({ tabs: '' })
            this.getStoreInfo()
            if (that.data.typeNum == 23) {
                this.getInfo()
            }

        })
        var phone = wx.getSystemInfoSync({})
        let iosphone = ["iPhone 5", 'iPhone 6/7/8', 'iPhone 6/7/8 Plus']
        if (phone.model.indexOf('iPhone') != -1) {  //包含  苹果手机的前提下判断
            for (var i = 0; i < iosphone.length; i++) {
                if (phone.model == iosphone[i]) {
                    this.setData({
                        bottoms: 0
                    })
                    return
                } else if (phone.model !== iosphone[i]) {
                    this.setData({
                        bottoms: 63
                    })
                }
            }
        }

    },
    getInfo() {
        const that = this
        const tempJson = {
            storeId: that.data.storeId
            // storeId:91
        }
        common.post('/store/getVenueMainPageInfo', tempJson,
            function (res) {
                if (res.status === 200) {
                    console.log(res);
                    that.setData({
                        storeInfos: res.data,
                        isFyApp: false
                    })
                    if (that.data.moduleType == 2) { // 文化场馆
                        that.setData({
                            articleList: res.data.articleList,
                        })
                    }
                } else {
                    common.showRequestErrMsg(res)
                }
            },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },
    getStoreInfo() { // 初始化店铺信息
        const that = this
        const tempJson = {
            storeId: that.data.storeId
        }
        common.post('/store/mainPageInfo', tempJson,
            function (res) {
                if (res.status === 200) {
                    that.setData({
                        storeInfo: res.data,
                    })
                    if (that.data.moduleType == 2) { // 文化场馆
                        that.setData({
                            articleList: res.data.articleList,
                        }, () => {

                        })
                    }
                } else {
                    common.showRequestErrMsg(res)
                }
            },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },
    toArticle(e) { // 跳转文章详情
        let {
            item
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/discovery/articleDetail/index?id=' + item.id,
        })
    },
    toBookPage(e) { // 立即预订
        let {
            item
        } = e.currentTarget.dataset
        let storeType = this.data.storeInfo.store.storeType
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAreaChooseTickets/index?ticketId=' + item.id +
                '&storeId=' + this.data.storeId +
                '&ticketName=' + item.ticketName +
                '&storeName=' + this.data.storeInfo.store.name +
                '&ticketSource=' + item.ticketSource +
                '&storeType=' + storeType
        })
    },
    toReservationPage(e) { // 立即预订

        let {
            item
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: '../../../packageA/pages/Goods/reservation/index?venueId=' + item.id +
                '&storeId=' + this.data.storeId +
                '&name=' + item.name +
                '&storeName=' + this.data.storeInfo.store.name
        })
    },
    toGoodsDetail(e) { // 购买景区项目
        const item = e.currentTarget.dataset.item
        common.gotoGoodsPage(item)
    },
    toAlbum() { // 进入商家相册
        let backUpPicList = encodeURIComponent(JSON.stringify(this.data.storeInfo.backUpPicList))
        let backUpVideoList = encodeURIComponent(JSON.stringify(this.data.storeInfo.backUpVideoList))
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAreaPhotoAlbum/index?backUpPicList=' + backUpPicList + '&backUpVideoList=' + backUpVideoList,
        })
    },
    mustKnow(e) { // 购买须知弹框
        let {
            item
        } = e.currentTarget.dataset
        this.setData({
            showMustKnow: true,
            currentPrebuy: item,
            ticketName: item.ticketName
        })
    },
    closeMustKnow() { // 关闭购买须知弹框
        this.setData({
            showMustKnow: false
        })
    },
    openCouponsPanel() { // 打开优惠券弹框
        this.setData({
            showCouponsPanel: true
        })
    },

    closeCouponsPanel() { // 关闭优惠券弹框
        this.setData({
            showCouponsPanel: false
        })
    },
    bannerChange(e) { // 轮播滚动触发
        let {
            current
        } = e.detail
        this.setData({
            currentBanner: current + 1
        })
    },
    onChangeTabbar(e) { //切换底部导航
        const index = e.detail.index
        const url = e.detail.url
        if (index != this.data.active) {
            utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
        }
    },
    onChangeTabbarc(e) { //切换底部导航
        const index = e.detail.index
        const url = e.detail.url
        if (index == 2 && this.data.tabs == 'venue') {
            this.setData({
                active: index
            })
            return
        }
        if (index != this.data.active) {
            utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
        }
    },
    showStoreIntro() {
        this.storeIntroduce(0)
    },
    storeIntroduce(type = 0) {
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAreaIntro/index?storeId=' + this.data.storeId
                + '&type=' + type
                + '&moduleType=' + this.data.moduleType
                + '&title=' + this.data.storeInfo.store.name
        })
    },
    mapGuide() {
        if (this.data.storeInfo.store.ypoint && this.data.storeInfo.store.xpoint) {
            wx.openLocation({
                latitude: Number(this.data.storeInfo.store.ypoint),
                longitude: Number(this.data.storeInfo.store.xpoint),
                name: this.data.storeInfo.store.name ? this.data.storeInfo.store.name : null,
                address: this.data.storeInfo.store.address ? this.data.storeInfo.store.address : null,
                scale: 16
            })
        } else {
            common.showToast('未获取到位置信息')
        }
    },
    goBack() {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
        if (prevPage) {
            wx.navigateBack({
                delta: 1
            })
        } else {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        }
    },
    showMore() { // 查看更多
        if (this.data.ticketShowNum == 4) {
            this.setData({
                ticketShowNum: this.data.storeInfo.ticketList.length
            })
        } else {
            this.setData({
                ticketShowNum: 4
            })
        }
    },
    callPhone(e) {
        let mobile = e.currentTarget.dataset.mobile
        wx.makePhoneCall({
            phoneNumber: mobile,
        })
    },
    onShareAppMessage: function () {
        return {
            title: '飞羊精选',
            path: 'pages/ScenicArea/ScenicAreaMainPage/index?id=' + this.data.storeId,
            imageUrl: '',
            success: (res) => { },
            fail: (res) => {
                // 分享失败
            }
        }
    },
    onShow() {
        if (this.data.moduleType == 2) {
            this.setData({
                tabs: 'venue',
                isplay: false
            })
        }
    }

})